<?php

require_once 'db_connect.php';
require_once 'Slim/Slim.php';
/*
 * Creating new Slim application
 */
$app = new Slim();
$db = new DB_CONNECT();
/*
 * Get topplace (depending on whether ID was provided)
 */

$app->get('/newfeed(/:id)', function ($id = null) use ($app) {
    $data = array();
    if ($id === null) { 
		
    } else {
        $data = array();
		$query = 'SELECT place.place_id, place.place_name, place.address, place.url_avatar, place.name_avatar, comment.detail_comment, comment.time_comment, comment.comment_id, comment.type FROM comment, place WHERE comment.user_id_zing ='.$id
                . ' and comment.place_id = place.place_id ORDER BY comment.time_comment DESC LIMIT 5';
		$result = mysql_query($query) or die(mysql_error());
		if (mysql_num_rows($result) > 0) {			
                while ($row = mysql_fetch_array($result)) {
					$data[] = array(
					'place_id' =>			$row['place_id'],
					'place_name' =>     	$row['place_name'],
					'address' =>       	$row['address'],
					'url_avatar' =>			$row['url_avatar'],
					'name_avatar' =>			$row['name_avatar'],
					'detail_comment' =>		$row['detail_comment'],
					'time_comment' =>		$row['time_comment'],
					'comment_id' =>		$row['comment_id'],
					'type' =>		$row['type']
				);
                }
        } 
    }
    $app->response()->header('Content-Type', 'application/json');
    echo json_encode($data);
});

/*
 * Runing the Slim app
 */
$app->run();